﻿/********
* 
* 
*  Description:
*  Create By kain.hong at 7/23/2010 3:49:20 PM
*  
*
*  Revision History:
*  Date                  Who                 What
*  
* 
*/
#region namespace

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Data;
using System.Data.Objects.DataClasses;
using System.Data.Metadata.Edm;
using System.Data.EntityClient;
using System.Data.Common;
using System.Linq.Expressions;

#endregion


namespace Cherry.Agile.Data.Repository
{
    /// <summary>
    /// 扩展方法集
    /// </summary>
    public static class Helper
    {
        /// <summary>
        /// 为ObjectSet增加添加ObjectParameter的扩展方法
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="os">当前的ObjectSet <see cref="ObjectSet"/></param>
        /// <param name="name">参数名称</param>
        /// <param name="value">参数值</param>
        /// <returns>添加的ObjectParameter <see cref="ObjectParameter"/></returns>
        public static ObjectParameter AddParameter<T>(this ObjectSet<T> os, string name, object value)  where T:class
        {
            var p = new ObjectParameter(name, value);
            os.Parameters.Add(p);
            return p;
        }

        /// <summary>
        /// Where 扩展，当满足conditon条件时则Append Where条件
        /// </summary>
        /// <typeparam name="T">查询类型</typeparam>
        /// <param name="query"><see cref="IQueryable"/></param>
        /// <param name="codition">条件</param>
        /// <param name="where">查询条件</param>
        /// <returns><see cref="IQueryable"/></returns>
        /// <example>
        /// <code><![CDATA[
        ///  var query = this.CreateQuery<CollectorInfo>();
        ///
        /// query = query.IfWhere(!string.IsNullOrEmpty(collectorCode), c => c.CollectorCode == collectorCode)
        ///     .IfWhere(type>0 ,c => c.Type == type )
        ///     .IfWhere(!string.IsNullOrEmpty(blueTooth),c => c.BlueTooth == blueTooth)
        ///     .Paging(paging);
        /// ]]>
        /// </code>
        /// </example>
        public static IQueryable<T> IfWhere<T>(this IQueryable<T> query, bool codition, Expression<Func<T, bool>> where)
        {
            if (codition)
                return query.Where(where);
            return query;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <param name="params"></param>
        /// <returns></returns>
        internal static DbParameter[] GetDbParameters<T>(this ObjectQuery<T> query)
        {
            var cmd = (query.Context.Connection as EntityConnection).StoreConnection.CreateCommand();
            var parameters = query.Parameters.Select(c =>
            {
                var p = cmd.CreateParameter();
                p.ParameterName = c.Name;
                p.Value = c.Value;
                return p;
            });
            return parameters.ToArray();
        }

        public static T GetValue<T>(this IDataRecord dr, string field)
        {
            var val = dr[field];
            if (val == DBNull.Value)
                return default(T);
            return (T)dr[field];
        }

    }
}
